//
//  WJJCalendarDayView.m
//  WJJCalendarView
//  日期显示视图
//
//  Created by JunWang on 16/3/7.
//  Copyright (c) 2016年 Insigma Hengtian Software Co.,Ltd  All rights reserved.
//


#import "WJJCalendarDayView.h"
#import "NSDate+WJJCalendarView.h"
#import "UIView+WJJUIViewExt.h"
#import "UIColor+ExtUIColor.h"

#define TextFontSize 17 //日期字体大小
#define IndexFontSize 8 //下标字体初始大小

@interface WJJCalendarDayView ()
@property (nonatomic, assign) CGRect clickZone;
@end




@implementation WJJCalendarDayView

{

    __strong NSCalendar *_calendar;
    __strong NSDate *_dayAsDate;
    __strong NSDateComponents *_day;
    __strong NSString *_labelText;
}


#pragma mark - Memory management

- (void)dealloc
{
}


#pragma mark - Initialisation

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self != nil) {
        self.backgroundColor = [UIColor whiteColor];
        _positionInWeek = WJJCalendarDayViewMidWeek;
        _festiType = WJJFestivalNone;
    }
    return self;
}


#pragma mark setter

- (void)setSelectionState:(WJJCalendarDayViewSelectionState)selectionState
{
    _selectionState = selectionState;
    [self setNeedsDisplay];
}

- (void)setDay:(NSDateComponents *)day
{
    _calendar = [day calendar];
    _dayAsDate = [day date];
    _day = nil;
    _labelText = [NSString stringWithFormat:@"%ld", (long)day.day];
}


#pragma mark getter

- (NSDateComponents*)day
{
    if (_day == nil) {
        _day = [_dayAsDate dslCalendarView_dayWithCalendar:_calendar];
    }
    
    return _day;
}

- (NSDate*)dayAsDate
{
    return _dayAsDate;
}


- (void)setInCurrentMonth:(BOOL)inCurrentMonth
{

   _inCurrentMonth = inCurrentMonth;

   [self setNeedsDisplay];
 
}

#pragma mark UIView methods
/**
 * 画图方法
 */
- (void)drawRect:(CGRect)rect
{
    if ([self isMemberOfClass:[WJJCalendarDayView class]]) {
        // 只有对WJJCalendarDayView子类执行绘制命令，否则默认
        [self drawBackground];
        
        if ([WJJCalendarLogic isToday:self.day]) {
            //如果是今天打上今天的标志
            [self drawTodayIndex];
            
        }
        
        [self drawDayNumber];
    }
}

/**
 * 选中圆点区域
 */
- (CGRect)clickZone
{
    if (_clickZone.size.height == 0) {
        return CGRectInset(self.bounds , self.width/6, self.width/6 + self.bounds.size.height/2 - self.bounds.size.width/2);
    }
    return _clickZone;
}

#pragma mark Drawing

/**
 * 画Day视图背景
 */
- (void)drawBackground
{
    if (self.selectionState == WJJCalendarDayViewNotSelected) {
        if (self.isInCurrentMonth) {
            //为当前月的日期设置为紫色背景
            [[UIColor customPurple] setFill];
        }
        else {
            //非当前月的日期设置为紫色背景
            [[UIColor customPurple] setFill];

            
        }
        UIRectFill(self.bounds);
    } else {
        CGRect clickZone = self.clickZone;
        switch (self.selectionState) {
            case WJJCalendarDayViewNotSelected:
                break;
                
            case WJJCalendarDayViewStartOfSelection:
                [[UIColor customPurple] setFill];
                UIRectFill(self.bounds);
                [[UIColor customTranGreen] setFill];//这里要改成半透明绿色
                UIRectFill(CGRectMake(self.bounds.size.width/2, (self.bounds.size.height - clickZone.size.height)/2, self.bounds.size.width/2,clickZone.size.height));
                CGContextRef ctxS = UIGraphicsGetCurrentContext();
                CGContextSetRGBFillColor(ctxS, 80.0/255, 209.0/255, 194.0/255, 1);
                CGContextFillEllipseInRect(ctxS,clickZone);
                UIGraphicsEndImageContext();
                break;
                
            case WJJCalendarDayViewEndOfSelection:
                [[UIColor customPurple] setFill];
                UIRectFill(self.bounds);
                [[UIColor customTranGreen] setFill];//这里要改成半透明绿色
                UIRectFill(CGRectMake(0, (self.bounds.size.height - clickZone.size.height)/2, self.bounds.size.width/2,clickZone.size.height));
                CGContextRef ctxE = UIGraphicsGetCurrentContext();
                CGContextSetRGBFillColor(ctxE, 80.0/255, 209.0/255, 194.0/255, 1);
                CGContextFillEllipseInRect(ctxE,clickZone);
                UIGraphicsEndImageContext();
                break;
                
            case WJJCalendarDayViewWithinSelection:
                [[UIColor customPurple] setFill];
                UIRectFill(self.bounds);
                [[UIColor customTranGreen] setFill];//这里要改成半透明绿色
                UIRectFill(CGRectMake(0, (self.bounds.size.height - clickZone.size.height)/2, self.bounds.size.width,clickZone.size.height));
                UIGraphicsEndImageContext();
                break;
                
            case WJJCalendarDayViewWholeSelection:
                //单个选中状态下填充背景
                [[UIColor customPurple] setFill];
                UIRectFill(self.bounds);
                CGContextRef ctxW = UIGraphicsGetCurrentContext();
                CGContextSetRGBFillColor(ctxW, 80.0/255, 209.0/255, 194.0/255, 1);
                CGContextFillEllipseInRect(ctxW,clickZone);
                UIGraphicsEndImageContext();
                break;
        }
    }
 
}


/**
 * 画day视图数字
 */
- (void)drawDayNumber
{
    if ((self.positionInWeek == WJJCalendarDayViewStartOfWeek || self.positionInWeek == WJJCalendarDayViewEndOfWeek || [WJJCalendarLogic needRest:self.day]) && self.selectionState == WJJCalendarDayViewNotSelected)
    {
        [[UIColor restDayTextColor]set];
        
    }else if (self.selectionState == WJJCalendarDayViewNotSelected) {
        [[UIColor whiteColor]set];//设置未选中日期字体颜色
    }
    else {
        [[UIColor whiteColor] set];//设置选中日期字体颜色
    }
    UIFont *textFont = [UIFont systemFontOfSize:TextFontSize];//设置日期字体
    CGSize textSize = [_labelText sizeWithFont:textFont];
    
    CGRect textRect = CGRectMake(ceilf(CGRectGetMidX(self.bounds) - (textSize.width / 2.0)), ceilf(CGRectGetMidY(self.bounds) - (textSize.height / 2.0)), textSize.width, textSize.height);
    if (self.isInCurrentMonth)//只绘制本月的日期
    {
        [_labelText drawInRect:textRect withFont:textFont];
        if ([WJJCalendarLogic checkFestival:self.day] != WJJFestivalNone) {
            [self drawFestibalIndex:[WJJCalendarLogic checkFestival:_day]];
            
        }
    }
}

/**
 * 画节日下标
 */
- (void)drawFestibalIndex:(WJJFestivalType)festiType
{
    NSString *type = [NSString string];
    switch (festiType) {
        case WJJSpringFestival:
            type = @"Spring Festival";
            break;
        case WJJArmyDay:
            type = @"Army Day";
            break;
        case WJJLaborDay:
            type = @"Labor Day";
            break;
        case WJJWomenDay:
            type = @"Women's Day";
            break;
        case WJJYouthDay:
            type = @"Youth Day";
            break;
        case WJJSingleDay:
            type = @"Single's Day";
            break;
        case WJJNewYearDay:
            type = @"New Year Day";
            break;
        case WJJTeacherDay:
            type = @"Teachers' Day";
            break;
        case WJJChildrenDay:
            type = @"Children's Day";
            break;
        case WJJNationalDay:
            type = @"National Day";
            break;
        case WJJPartyBirthday:
            type = @"Party Birthday";
            break;
        case WJJLanternFestival:
            type = @"Lantern Festival";
            break;
        case WJJQingMingFestival:
            type = @"Qing Ming Festival";
            break;
        case WJJDragonBoatFestival:
            type = @"Dragon Boat Festival";
            break;
        case WJJMidAutumnFestival:
            type = @"Mid Autumn Day";
            break;
        case WJJDoubleNinthFestival:
            type = @"Double Ninth Festival";
            break;
        default:
            break;
    }
    [self drawIndexWord:type withColor:[UIColor whiteColor]];
}

/**
 * 画今日下标
 */
- (void)drawTodayIndex
{
    
    [self drawIndexWord:@"Today" withColor:[UIColor redColor]];
}

/**
 * 画下标方法
 */
- (void)drawIndexWord:(NSString *)str withColor:(UIColor *)indexColor
{
    
    NSString *index = [NSString stringWithFormat:@"●%@",str];
    [indexColor set];
    UIFont *indexFont = [UIFont systemFontOfSize:IndexFontSize];
    CGSize indexSize = [index sizeWithFont:indexFont];
    
    CGRect indexRect = CGRectMake(ceilf(CGRectGetMidX(self.bounds) - (indexSize.width / 2.0)), ceilf(CGRectGetMidY(self.bounds) + self.clickZone.size.height/2), indexSize.width, indexSize.height);
    
    if (indexRect.size.width > self.bounds.size.width) {
        CGFloat adjFontSize = IndexFontSize;
        while (indexRect.size.width > self.bounds.size.width) {
            adjFontSize --;
            indexFont = [UIFont systemFontOfSize:adjFontSize];
            indexSize = [index sizeWithFont:indexFont];
            indexRect = CGRectMake(ceilf(CGRectGetMidX(self.bounds) - (indexSize.width / 2.0)), ceilf(CGRectGetMidY(self.bounds) + self.clickZone.size.height/2), indexSize.width, indexSize.height);
        }
    }
    [index drawInRect:indexRect withFont:indexFont];
    
}





@end
